using System;
using System.Collections.Generic;
using System.Text;
using SqlTools.DataAccessLibrary;
using System.Data.SqlClient;
using System.Data;

namespace SqlTools.SampleDAL.Data
{
	public class DbUser : DbBase<UserPrimaryKey, User>
	{
		public DbUser()
			: base("daltest")
		{
		}

		public DbUser(string connectionStringName)
			: base(connectionStringName)
		{
		}

		#region Select
		public override User SelectByPK(UserPrimaryKey key)
		{
			User user = null;
			using ( SqlConnection conn = new SqlConnection(_connectionString) )
			{
				SqlCommand cmd = new SqlCommand("daltest_User_SelectByPK", conn);
				cmd.CommandType = CommandType.StoredProcedure;

				cmd.Parameters.Add("@UserID", SqlDbType.Int).Value = key.UserID;

				SqlDataReader sdr = cmd.ExecuteReader();

				if ( sdr.Read() )
				{
					user = User.CreateFromReader(sdr);
					user._ConnectionStringName = _connectionStringName;
				}
				sdr.Close();
			}

			return user;
		}

		public bool SelectByPK(User user, UserPrimaryKey key)
		{
			bool objectFound = false;
			using ( SqlConnection conn = new SqlConnection(_connectionString) )
			{
				SqlCommand cmd = new SqlCommand("daltest_User_SelectByPK", conn);
				cmd.CommandType = CommandType.StoredProcedure;

				cmd.Parameters.Add("@UserID", SqlDbType.Int).Value = key.UserID;

				conn.Open();

				SqlDataReader sdr = cmd.ExecuteReader();

				if ( sdr.Read() )
				{
					objectFound = true;
					User.FillFromReader(user, sdr);
					user._ConnectionStringName = _connectionStringName;
				}
				sdr.Close();

				conn.Close();
			}

			return objectFound;
		}
		#endregion

		#region Insert
		public override UserPrimaryKey Insert(User user, SqlConnection conn, SqlTransaction trans)
		{
			user._ConnectionStringName = _connectionStringName;

			bool destroyConn = false;

			if ( conn == null )
			{
				destroyConn = true;
				conn = new SqlConnection(_connectionString);
			}

			try
			{
				SqlCommand cmd = new SqlCommand("daltest_User_Insert", conn);
				cmd.CommandType = CommandType.StoredProcedure;
				cmd.Transaction = trans;

				if ( conn.State != ConnectionState.Open )
				{
					conn.Open();
				}

				cmd.Parameters.Add("@UserID", SqlDbType.Int).Direction = ParameterDirection.Output;
				cmd.Parameters.Add("@Username", SqlDbType.NVarChar, 50).Value = user.Username;
				cmd.Parameters.Add("@Password", SqlDbType.NVarChar, 50).Value = user.Password;

				cmd.ExecuteNonQuery();

				if ( cmd.Parameters["@UserID"].Value != DBNull.Value )
				{
					user.Key.UserID = (int)cmd.Parameters["@UserID"].Value;
				}

				user._UpdateState(ObjectState.None);
			}
			finally
			{
				if ( destroyConn && conn.State == ConnectionState.Open )
				{
					conn.Close();
				}

				if ( destroyConn )
				{
					conn.Dispose();
				}
			}

			return user.Key;
		}
		#endregion

		#region Update
		public override int Update(User user, SqlConnection conn, SqlTransaction trans)
		{
			user._ConnectionStringName = _connectionStringName;

			bool destroyConn = false;

			if ( conn == null )
			{
				destroyConn = true;
				conn = new SqlConnection(_connectionString);
			}

			try
			{
				SqlCommand cmd = new SqlCommand("daltest_User_Update", conn);
				cmd.CommandType = CommandType.StoredProcedure;
				cmd.Transaction = trans;

				if ( conn.State != ConnectionState.Open )
				{
					conn.Open();
				}

				cmd.Parameters.Add("@UserID", SqlDbType.Int).Value = user.Key.UserID;
				cmd.Parameters.Add("@Username", SqlDbType.NVarChar, 50).Value = user.Username;
				cmd.Parameters.Add("@Password", SqlDbType.NVarChar, 50).Value = user.Password;

				int rows = cmd.ExecuteNonQuery();

				user._UpdateState(ObjectState.None);

				return rows;
			}
			finally
			{
				if ( destroyConn && conn.State == ConnectionState.Open )
				{
					conn.Close();
				}

				if ( destroyConn )
				{
					conn.Dispose();
				}
			}
		}
		#endregion

		#region Delete
		public override int Delete(User user, SqlConnection conn, SqlTransaction trans)
		{
			bool destroyConn = false;

			if ( conn == null )
			{
				destroyConn = true;
				conn = new SqlConnection(_connectionString);
			}

			try
			{
				SqlCommand cmd = new SqlCommand("daltest_User_Delete", conn);
				cmd.CommandType = CommandType.StoredProcedure;
				cmd.Transaction = trans;

				if ( conn.State != ConnectionState.Open )
				{
					conn.Open();
				}

				cmd.Parameters.Add("@UserID", SqlDbType.Int).Value = user.Key.UserID;

				int rows = cmd.ExecuteNonQuery();

				user._UpdateState(ObjectState.Deleted);

				return rows;
			}
			finally
			{
				if ( destroyConn && conn.State == ConnectionState.Open )
				{
					conn.Close();
				}

				if ( destroyConn )
				{
					conn.Dispose();
				}
			}
		}
		#endregion
	}
}
